package md.lanjing.com.module_deal_center.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.TimeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_deal_center.R;
import md.lanjing.com.module_deal_center.R2;
import md.lanjing.com.module_deal_center.bean.BuyBillDetailResponce;
import md.lanjing.com.module_deal_center.bean.DealCenterBuyListResponce;
import com.lxh.baselibray.constant.Constant;
import md.lanjing.com.module_deal_center.contact.BuyDetailContact;
import retrofit2.Response;

/**
 * 购买订单详情
 */
public class BuyDetailActivity extends MvpActivity<BuyDetailContact.BuyDetailPresent> implements BuyDetailContact.IBuyDetailView {
    @BindView(R2.id.tv_seller)
    TextView tvSeller;
    @BindView(R2.id.tv_coin_type)
    TextView tvCoinType;
    @BindView(R2.id.et_numbel)
    EditText etNumbel;
    @BindView(R2.id.tv_cny)
    TextView tvCny;
    @BindView(R2.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R2.id.tv_order_number)
    TextView tvOrderNumber;

    public static String ORDER_BEAN="order_bean";
    DealCenterBuyListResponce.DataBean orderBean;
    @Override
    public void initData(Bundle savedInstanceState) {
        String json =getIntent().getStringExtra(ORDER_BEAN);
        orderBean=new Gson().fromJson(json,DealCenterBuyListResponce.DataBean.class);
        if (!ObjectUtils.isEmpty(orderBean)){
            String ownerPhone = orderBean.getOwnerPhone();
            if (!ObjectUtils.isEmpty(ownerPhone)&&ownerPhone.length()>7){
                tvSeller.setText(ownerPhone.substring(0,3)+"****"+ownerPhone.substring(ownerPhone.length()-4,ownerPhone.length()));
            }

            tvCoinType.setText(orderBean.getType());
            tvOrderTime.setText(TimeUtils.millis2String(orderBean.getCreateTime()));
            tvOrderNumber.setText(orderBean.getId());
        }

        etNumbel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (ObjectUtils.isEmpty(s))return;
                Double count = Double.valueOf(s.toString());
                tvCny.setText(count*Double.valueOf(orderBean.getCnyPrice())+"");
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_buy_detail;
    }


    @OnClick(R2.id.btn_confirm)
    public void onViewClicked() {
        String num = etNumbel.getText().toString();
        if (ObjectUtils.isEmpty(num)){
            ToastUtils.showLongToast(context,"请输入购买数量");
            return;
        }
        showDealDialog(num);
    }

    AlertDialog dealDialog = null;
    private void showDealDialog(String num) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw=dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd=psw.getPsw();
                    mPresent.lockBill(context,dealPwd,Double.valueOf(num),orderBean.getId());
                    dealDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }

    @Override
    public void getLockBillData(Response<BuyBillDetailResponce> response) {
        if (response.body().getCode()== Constant.SUCCESS_CODE){
            showRemindDialog("订单锁定成功,去查看");
        }else {
            ToastUtils.showLongToast(context,response.body().getMsg());
        }
    }

    @Override
    protected BuyDetailContact.BuyDetailPresent createPresent() {
        return new BuyDetailContact.BuyDetailPresent();
    }

    AlertDialog remindDialog = null;
    private void showRemindDialog(String content) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_reimind)
                .setText(R.id.tv_content, content)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    Intent intent = new Intent(context, OrderActivity.class);
                    intent.putExtra(OrderActivity.FROM,1);
                    startActivity(intent);
                    remindDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> remindDialog.dismiss());
        remindDialog = builder.create();
        remindDialog.show();
    }
}
